﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


using DotNetNuke;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using Microsoft.ApplicationBlocks.Data;
using System.Text;
using CWCare.Modules.cwcProvider.Components;
using System.Data;
using System.Data.Linq;

namespace CWCare.Modules.cwcProvider
{
    /// <summary>
    /// Update, Insert for Provider
    /// </summary>
    public partial class UpdateProvider : PortalModuleBase
    {
        private const string InsertProvider = "Insert Provider";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                if (this.IsEditable == false)
                {
                    string paraId = string.Empty;
                    if (!string.IsNullOrEmpty(Request.Params.Get("pid")))
                    {
                        paraId = "&pid=" + Request.Params.Get("pid");
                    }

                    Response.Redirect("~/Default.aspx?TabId=" + this.TabId + paraId);
                }

                this.SetLisrProviderId();

                if (string.IsNullOrEmpty(this.Request.Params.Get("pid")))
                {
                    this.SetStatusInsertProvider();
                }
                else
                {
                    this.SetStatusUpdateProvider(this.Request.Params.Get("pid"));
                }
            }
        }

        private void SetStatusInsertProvider()
        {
            //btnUpdate.Text = "Insert";
            btnDelete.Visible = false;

            txtBio.Text = string.Empty;
            txtContentSearch.Text = string.Empty;
            txtBioCopy.Text = string.Empty;
            txtEmail.Text = string.Empty;
            txtFirstName.Text = string.Empty;
            txtHours.Text = string.Empty;
            txtLastName.Text = string.Empty;
            txtphoto.Text = string.Empty;
            txtWebsite.Text = string.Empty;
        }

        private void SetStatusUpdateProvider(string spid)
        {
            //btnUpdate.Text = "Update";
            btnDelete.Visible = true;
            int pid = -1;
            try
            {
                pid = int.Parse(spid);
            }
            catch
            {
                Response.Redirect("~/Default.aspx?TabId=" + this.TabId);
            }

            this.GetProvider(pid);
        }

        private void SetLisrProviderId()
        {
            cwcareDataContext context = new cwcareDataContext();
            var info = (from pro in context.GetTable<cwc_Provider>()
                        select new ListItem(pro.fName + " " + pro.lName, pro.providerID.ToString()));

            cwc_Provider pr = new cwc_Provider();
            ddlProviderId.DataSource = info;
            ddlProviderId.DataTextField = "text";
            ddlProviderId.DataValueField = "value";
            ddlProviderId.DataBind();
            ddlProviderId.Items.Insert(0, new ListItem(InsertProvider, InsertProvider));
        }

        private int GetMaxProviderId()
        {
            cwcareDataContext context = new cwcareDataContext();
            var max = (from pro in context.GetTable<cwc_Provider>()
                       select pro.providerID).Max();

            return max + 1;
        }

        private int GetIndexItem(DropDownList ddl, string value)
        {
            int index = -1;
            var info = (from a in ddl.Items.Cast<ListItem>()
                        where a.Value == value || a.Text == value
                        select a).ToList<ListItem>();
            if (info.Count > 0)
            {
                index = ddl.Items.IndexOf(info[0]);
            }
            return index;
        }

        private void GetProvider(int pid)
        {
            cwcareDataContext context = new cwcareDataContext();
            var info = (from pro in context.GetTable<cwc_Provider>()
                        where pro.providerID == pid
                        select pro).ToList<cwc_Provider>();

            if (info.Count > 0)
            {
                ddlProviderId.SelectedIndex = this.GetIndexItem(ddlProviderId, pid.ToString());
                txtBio.Text = info[0].bio;
                txtContentSearch.Text = info[0].contentSearch;
                txtBioCopy.Text = info[0].bio_copy;
                txtEmail.Text = info[0].email;
                txtFirstName.Text = info[0].fName;
                txtHours.Text = info[0].hours;
                txtLastName.Text = info[0].lName;
                txtphoto.Text = info[0].photoName;
                txtWebsite.Text = info[0].website;
            }
            else
            {
                this.SetStatusInsertProvider();
            }
        }

        private void SetProvider(cwc_Provider pro)
        {
            pro.bio = txtBio.Text;
            pro.contentSearch = txtContentSearch.Text;
            pro.bio_copy = txtBioCopy.Text;
            pro.email = txtEmail.Text;
            pro.fName = txtFirstName.Text;
            pro.hours = txtHours.Text;
            pro.lName = txtLastName.Text;
            pro.photoName = txtphoto.Text;
            pro.website = txtWebsite.Text;
        }

        protected void btnUpdate_OnClick(object sender, EventArgs e)
        {
            cwcareDataContext context = new cwcareDataContext();
            int pid = -1;

            if (ddlProviderId.SelectedValue.Equals(InsertProvider))
            {
                cwc_Provider pro = new cwc_Provider();
                this.SetProvider(pro);
                //pid = this.GetMaxProviderId();
                //pro.providerID = pid;
                context.cwc_Providers.InsertOnSubmit(pro);
                try
                {
                    context.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    context.ChangeConflicts.ResolveAll(RefreshMode.KeepChanges);
                }
                pid = this.GetMaxProviderId()-1;
            }
            else
            {
                pid = int.Parse(ddlProviderId.SelectedValue);

                var info = (from pro in context.GetTable<cwc_Provider>()
                            where pro.providerID == pid
                            select pro).ToList<cwc_Provider>();
                if (info.Count > 0)
                {
                    cwc_Provider pro = info[0];
                    this.SetProvider(pro);
                    try
                    {
                        context.SubmitChanges();
                    }
                    catch (ChangeConflictException)
                    {
                        context.ChangeConflicts.ResolveAll(RefreshMode.KeepChanges);
                    }
                }
            }

            Response.Redirect("~/Default.aspx?TabId=" + this.TabId + "&pid=" + pid.ToString());
        }

        protected void btnDelete_OnClick(object sender, EventArgs e)
        {
            int pid = int.Parse(this.Request.Params.Get("pid"));
            if (!ddlProviderId.SelectedValue.Equals(InsertProvider))
            {
                pid = int.Parse(ddlProviderId.SelectedValue);
            }           
            cwcareDataContext context = new cwcareDataContext();
            var info = (from pro in context.GetTable<cwc_Provider>()
                        where pro.providerID == pid
                        select pro).ToList<cwc_Provider>();
            if (info.Count > 0)
            {
                cwc_Provider pro = info[0];
                context.cwc_Providers.DeleteOnSubmit(pro);
                try
                {
                    context.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    context.ChangeConflicts.ResolveAll(RefreshMode.KeepChanges);
                }
            }

            Response.Redirect("~/Default.aspx?TabId=" + this.TabId);

        }

        protected void btnCancel_OnClick(object sender, EventArgs e)
        {
            string paraId = string.Empty;
            if (!ddlProviderId.SelectedValue.Equals(InsertProvider))
            {
                paraId = "&pid=" + ddlProviderId.SelectedValue;
            }
            else if (!string.IsNullOrEmpty(Request.Params.Get("pid")))
            {
                paraId = "&pid=" + Request.Params.Get("pid");
            }

            Response.Redirect("~/Default.aspx?TabId=" + this.TabId + paraId);
        }



        protected void ddlProviderId_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlProviderId.SelectedValue.Equals(InsertProvider))
            {
                this.SetStatusInsertProvider();
            }
            else
            {
                this.SetStatusUpdateProvider(ddlProviderId.SelectedValue);
            }
        }

    }


}